/**
 * Copyright (c) 2011, Traction Software, Inc. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 * 
 * Neither the name of the Traction Software, Inc. nor the names of
 * its contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.Copyright 2010 Traction Software,
 * Inc.
 */
package com.tractionsoftware.googleio.client;

import java.util.Date;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.RepeatingCommand;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;

/**
 * @author andy
 *
 */
    public class Countdown extends Composite {

    private Digit[] digits = new Digit[] {
	new Digit("s0"),
	new Digit("s1"),
	
	new Digit("m0"),
	new Digit("m1"),
	
	new Digit("h0"),
	new Digit("h1"),
	
	new Digit("d0"),
	new Digit("d1"),
	new Digit("d2") 
    };

    private long target;

    public Countdown(String id) {
        SimplePanel panel = new SimplePanel();
        Element panelElement = panel.getElement();
        panelElement.setId(id);
        panelElement.setClassName("digits");

        for (int i=digits.length-1; i >= 0; i--) {
            Digit d = digits[i];
            
            panelElement.appendChild(d.getElement());
            d.setValue(0);
            
            switch (i) {
            case 2:
            case 4:
            case 6:
                Element colon = DOM.createSpan();
                colon.setClassName("colon");
                colon.setInnerText(":");
                panelElement.appendChild(colon);
                break;
            }
        }        
        
        initWidget(panel);
    }
    
    public void start() {
        
        // delay this to create the initial animation from 000:00:00:00
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
            @Override
            public void execute() {
                update();
            }
        });
        
        // repeat
        Scheduler.get().scheduleFixedPeriod(new RepeatingCommand() {
            
            @Override
            public boolean execute() {
                update();
                return true;
            }
            
        }, 100);
    }
    
    public void update() {
        long now = new Date().getTime();
       
        int remain = (int) ((target - now) / 1000);
        
        int seconds = remain % 60;
        digits[0].setValue(digit(seconds, 1));
        digits[1].setValue(digit(seconds, 10));
        
        int minutes = (remain / 60) % 60;
        digits[2].setValue(digit(minutes, 1));
        digits[3].setValue(digit(minutes, 10));
        
        int hours = (remain / 60 / 60) % 24;
        digits[4].setValue(digit(hours, 1));
        digits[5].setValue(digit(hours, 10));
        
        int days = remain / 60 / 60 / 24;
        digits[6].setValue(digit(days, 1));
        digits[7].setValue(digit(days, 10));
        digits[8].setValue(digit(days, 100));
    }
    
    public final int digit(int number, int place) {
        return (number / place) % 10;
    }
    
    public void setDate(Date date) {
        if (date != null) {
            this.target = date.getTime();
        }
    }
    
}
